package com.imax.center.controller;

import com.imax.center.annotation.NoAuth;
import com.imax.center.common.Result;
import com.imax.center.service.CommonService;
import com.imax.center.service.DataCacheService;
import com.imax.center.service.MinioService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import com.imax.center.utils.MyFileUtils;
import com.imax.center.domain.H3yunRequestDomain;
import com.imax.center.common.ResultCode;
import com.alibaba.fastjson.JSON;
import com.imax.center.domain.AlarmConfig;
import com.alibaba.fastjson.JSONArray;
import java.io.IOException;
import java.util.List;

/**
 * 通用功能控制器
 */
@Api(tags = "通用功能API")
@RestController
@RequestMapping("/api/common")
@Slf4j
public class CommonController {

    @Autowired
    private CommonService commonService;

    @Autowired
    private MinioService minioService;

    @Autowired
    private DataCacheService dataCacheService;

    @NoAuth
    @ApiOperation(value = "氚云调用imaxCenter接口封装", notes = "氚云调用imaxCenter接口封装")
    @PostMapping("/invoke")
    public Result<String> invoke(@RequestBody H3yunRequestDomain requestBody) {
        if (requestBody == null) {
            return Result.error(ResultCode.PARAM_ERROR);
        }
        //判断密码是否正确
        if (!requestBody.getPwd().equals("imaxCenter")) {
            return Result.error("密码错误");
        }
        log.info("氚云调用imaxCenter接口封装:{}", JSON.toJSONString(requestBody));
        switch (requestBody.getMethod()) {
            // 刷新站点预警配置列表
            case "refresh-alarm-config":
                List<AlarmConfig> result1 = dataCacheService.refreshAlarmConfig();
                return Result.success(JSON.toJSONString(result1), "调用成功");
            // 刷新行政区划数据
            case "refresh-region-data":
                JSONArray result2 = dataCacheService.refreshRegionData();
                return Result.success(JSON.toJSONString(result2), "调用成功");
            // 刷新传感器设备数据
            case "refresh-sensor-data":
                JSONArray result3 = dataCacheService.refreshSensorDevice();
                return Result.success(JSON.toJSONString(result3), "调用成功");
            default:
                return Result.error("方法名错误");
        }
    }

    /**
     * 获取行政区划边界GeoJSON数据
     *
     * @param areaCode 行政区划代码
     * @return GeoJSON数据
     */
    @NoAuth
    @ApiOperation("获取行政区划边界GeoJSON数据")
    @GetMapping("/area/geo")
    public Object getAreaBoundary(@ApiParam(value = "行政区划代码", required = true) @RequestParam String areaCode) {
        return commonService.getAreaBoundary(areaCode);
    }


    // 刷新行政区划数据
    @ApiOperation("刷新行政区划数据")
    @GetMapping("/region/refresh")
    public Object refreshRegionData() {
        return dataCacheService.refreshRegionData();
    }

    // 刷新传感器设备数据
    @ApiOperation("刷新传感器设备数据")
    @GetMapping("/sensor/refresh")
    public Object refreshSensorData() {
        return dataCacheService.refreshSensorDevice();
    }

    // 刷新站点预警配置列表
    @ApiOperation("刷新站点预警配置列表")
    @GetMapping("/alarm/refresh")
    public Object refreshAlarmConfig() {
        return dataCacheService.refreshAlarmConfig();
    }

    /**
     * 上传图片
     * @param file 图片文件
     * @return 图片存储路径
     */
    @ApiOperation("上传图片")
    @PostMapping("/upload/image")
    public Result<String> uploadImage(@ApiParam(value = "图片文件", required = true) @RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return Result.error("请选择要上传的图片");
        }
        try {
            String imagePath = commonService.saveImage(file, null);
            return Result.success(imagePath);
        } catch (Exception e) {
            log.error("上传图片失败", e);
            return Result.error("上传图片失败" + e.getMessage());
        }
    }

    @ApiOperation("通过图片URL上传图片")
    @PostMapping("/upload/image/url")
    public Result<String> uploadImageByUrl(@ApiParam(value = "图片URL", required = true) @RequestParam String url, @ApiParam(value = "图片名称", required = false) @RequestParam(required = false) String fileName) throws IOException {
        // 将filePath转换为MultipartFile
        MultipartFile multipartFile = MyFileUtils.urlToMultipartFile(url);
        try {
            String imagePath = commonService.saveImage(multipartFile, fileName);
            return Result.success(imagePath);
        } catch (Exception e) {
            log.error("上传图片失败", e);
            return Result.error("上传图片失败" + e.getMessage());
        }
    }

    /**
     * 上传图片到minio
     * @param file 图片文件
     * @return 图片存储路径
     */
    @ApiOperation("上传图片到minio")
    @PostMapping("/upload/image/minio")
    public Result<String> uploadImageMinio(@ApiParam(value = "图片文件", required = true) @RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return Result.error("请选择要上传的图片");
        }
        try {
            String imagePath = minioService.upload(file);
            return Result.success(imagePath);
        } catch (Exception e) {
            log.error("上传图片minio失败", e);
            return Result.error("上传图片minio失败" + e.getMessage());
        }
    }

    @ApiOperation("通过图片URL上传图片到minio")
    @PostMapping("/upload/image/url/minio")
    public Result<String> uploadImageByUrlMinio(@ApiParam(value = "图片URL", required = true) @RequestParam String url, @ApiParam(value = "图片名称", required = true) @RequestParam(required = false) String fileName) throws IOException {
        // 将filePath转换为MultipartFile
        MultipartFile multipartFile = MyFileUtils.urlToMultipartFile(url);
        try {
            String imagePath = minioService.upload(multipartFile, fileName);
            return Result.success(imagePath);
        } catch (Exception e) {
            log.error("上传图片minio失败", e);
            return Result.error("上传图片minio失败" + e.getMessage());
        }
    }

    @ApiOperation("删除minio图片")
    @GetMapping("/delete/image/minio")
    public Result<String> deleteMinioImage(@ApiParam(value = "图片URL", required = true) @RequestParam String url) throws IOException {
        try {
            minioService.deleteFile(url);
            return Result.success("删除成功");
        } catch (Exception e) {
            log.error("删除minio图片失败", e);
            return Result.error("删除minio图片失败" + e.getMessage());
        }
    }

} 